const Stations = require("../model/stations.model.js");

class StationsService {
  //保存接头盒信息，如果没有companyId则为新增，否则为更新
  async saveStations({
    stationId,
    stationName,
    voltage,
    links,
    works,
    dataFlag,
    Latitude,
    Longitude,
    imageUrl,
    R,
    G,
    B,
    Z
  }) {
    var update = false;
    var stations = {};
    var whereOpt = {};
    if (stationId && stationId > 0) {
      update = true;
      whereOpt.stationId = stationId;
    }
    console.log(stationId);
    works && Object.assign(stations, { works: JSON.stringify(works) });
    stationId >= 0 && Object.assign(stations, { stationId });
    stationName && Object.assign(stations, { stationName });
    voltage && Object.assign(stations, { voltage });
    dataFlag >= 0 && Object.assign(stations, { dataFlag });
    Longitude && Object.assign(stations, { Longitude });
    Latitude && Object.assign(stations, { Latitude });
    links && Object.assign(stations, { links: JSON.stringify(links) });
    R && Object.assign(stations, { R });
    G && Object.assign(stations, { G });
    B && Object.assign(stations, { B });
    Z && Object.assign(stations, { Z });
    imageUrl && Object.assign(stations, { imageUrl });
    if (Object.keys(stations).length == 0) {
      return false;
    }
    var res;
    try {
      if (update) {
        res = await Stations.update(stations, { where: whereOpt });
        if (res[0] > 0) {
          stations.stationId = stationId;
          return JSON.parse(JSON.stringify(stations));;
        } else {
          return false;
        }
      } else {
        res = await Stations.create(stations);
        console.log(res);
        if (res.dataValues == undefined) {
          return false;
        } else {
          return JSON.parse(JSON.stringify(res.dataValues));
        }
      }
    } catch (error) {
      console.log(error);
      return null;
    }
  }
  //删除stations
  async eraseStationsById(stationId) {
    if (!stationId || stationId <= 0) {
      return false;
    }
    var res;
    try {
      res = Stations.destroy({ where: { stationId } });
      if (!res) {
        return false;
      }
      return true;
    } catch (error) {
      console.log(error);
      return null;
    }
  }
  /* 
删除指定条件的接头盒
*/
  async eraseStations({ stationsId }) {
    var whereOpt = {};
    stationsId && Object.assign(whereOpt, { stationsId });
    whereOpt.dataFlag = 1;
    if (Object.keys(whereOpt).length == 0) {
      return false;
    }
    var res;
    try {
      res = await Stations.destroy({ where: whereOpt });
      if (!res) {
        return false;
      } else {
        return true;
      }
    } catch (error) {
      console.log(error);
      return null;
    }
  }
  /* 
  删除所有stations
  */
  async eraseAllStations() {
    var whereOpt = {};
    whereOpt.dataFlag = 1;
    if (Object.keys(whereOpt).length == 0) {
      return false;
    }
    var res;
    try {
      res = await Stations.destroy({ where: whereOpt });
      if (!res) {
        return false;
      } else {
        return true;
      }
    } catch (error) {
      console.log(error);
      return null;
    }
  }
  /* 
 批量添加
 */
  async batchAddStations(arr) {
    if (!(arr instanceof Array) || arr.length == 0) {
      return false;
    }
    if (arr.length > 0) {
      for (var i = 0; i < arr.length; i++) {
        arr[i]['links'] = JSON.stringify(arr[i]['links']);
        arr[i]['works'] = JSON.stringify(arr[i]['works']);
      }
    }
    var res;
    try {
      res = await Stations.bulkCreate(arr, {
        fields: [
          "stationName",
          "voltage",
          "links",
          "longitude",
          "latitude",
          "works",
          "R",
          "G",
          "B",
          "imageUrl",
          "Z"
        ]
      });
      return JSON.parse(JSON.stringify(res));
    } catch (error) {
      console.log(error);
      return null;
    }
  }
  //查找指定ID的变电站
  async getStationById(stationId) {
    if (!stationId || stationId == undefined || stationId <= 0) {
      return false;
    }
    var whereOpt = {};
    whereOpt.stationId = stationId;
    var res;
    try {
      res = await Stations.findOne({
        where: whereOpt
      });
      if (!res || res == null) {
        return {};
      }
      return JSON.parse(JSON.stringify(res));
    } catch (error) {
      console.log(error);
      return null;
    }
  }

  /*
   *
   *@page:分页选项
   *@page.pageIndex:当前第几页
   *@page.pageSize:每页多少条记录
   *@page.pageCount:总共多少页
   *@page.sum:总共多少条记录
   */
  async getStation({ stationId, stationName, voltage, links }) {
    var whereOpt = {};
    stationId >= 0 && Object.assign(whereOpt, { stationId });
    stationName && Object.assign(whereOpt, { stationName });
    voltage && Object.assign(whereOpt, { voltage });
    links && Object.assign(whereOpt, { links });
    var res;
    try {
      res = await Stations.findAll({
        where: whereOpt,
        order: [["stationId", "ASC"]],
      });
      if (!res || res == null) {
        return [];
      }
      return JSON.parse(JSON.stringify(res));
    } catch (error) {
      console.log(error);
      return null;
    }
  }
}

module.exports = new StationsService();
